package com.yunc.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Function;
import com.google.common.collect.Maps;
import com.yunc.base.controller.CommonController;
import com.yunc.common.utils.R;
import com.yunc.modules.sys.entity.SysSchedule;
import com.yunc.modules.sys.service.ISysScheduleService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典码
 *
 * @author congcong tao
 * @date 2018/11/19 17:09
 */
@RestController
@RequestMapping("sysSchedule")
public class SysScheduleController extends CommonController {

    private static final Logger logger = LogManager.getLogger(SysScheduleController.class);

    @Autowired
    private ISysScheduleService sysScheduleService;

    @GetMapping("/list")
    public R list(){
        QueryWrapper<SysSchedule> ew = new QueryWrapper<>();
        List<SysSchedule> list = sysScheduleService.list(ew);
        Map<String, String[]> maps = new HashMap<>();
        if (list != null && list.size() > 0) {
            for (SysSchedule sysSchedule : list) {
                String[] split = sysSchedule.getTitle().split(",");
                maps.put(sysSchedule.getDay(), split);
            }
        }
        return R.ok().put("maps", maps);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody SysSchedule schedule){
        schedule.setCreateTime(new Date());
        boolean flag = sysScheduleService.saveSchedule(schedule);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @PutMapping(value = "/update")
    public R update(@RequestBody SysSchedule schedule){
        boolean flag = sysScheduleService.updateById(schedule);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}
